package com.fypg.module.medical.service.hospital;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.hospital.vo.*;
import com.fypg.module.medical.dal.dataobject.hospital.HospitalDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.hospital.HospitalMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 医院信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalMapper hospitalMapper;

    @Override
    public String createHospital(HospitalSaveReqVO createReqVO) {
        // 插入
        HospitalDO hospital = BeanUtils.toBean(createReqVO, HospitalDO.class);
        hospitalMapper.insert(hospital);
        // 返回
        return hospital.getId();
    }

    @Override
    public void updateHospital(HospitalSaveReqVO updateReqVO) {
        // 校验存在
        validateHospitalExists(updateReqVO.getId());
        // 更新
        HospitalDO updateObj = BeanUtils.toBean(updateReqVO, HospitalDO.class);
        hospitalMapper.updateById(updateObj);
    }

    @Override
    public void deleteHospital(String id) {
        // 校验存在
        validateHospitalExists(id);
        // 删除
        hospitalMapper.deleteById(id);
    }

    @Override
        public void deleteHospitalListByIds(List<String> ids) {
        // 校验存在
        validateHospitalExists(ids);
        // 删除
        hospitalMapper.deleteByIds(ids);
        }

    private void validateHospitalExists(List<String> ids) {
        List<HospitalDO> list = hospitalMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(HOSPITAL_NOT_EXISTS);
        }
    }

    private void validateHospitalExists(String id) {
        if (hospitalMapper.selectById(id) == null) {
            throw exception(HOSPITAL_NOT_EXISTS);
        }
    }

    @Override
    public HospitalDO getHospital(String id) {
        return hospitalMapper.selectById(id);
    }

    @Override
    public PageResult<HospitalDO> getHospitalPage(HospitalPageReqVO pageReqVO) {
        return hospitalMapper.selectPage(pageReqVO);
    }

}